package com.itheima.web;

import com.itheima.Service.UserService;
import com.itheima.dao.pojo.User;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/updateUserByIDServlet")
public class UpdateUserByIDServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取传递过来的数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        //创建用户对象
        User user = new User();
        try {
            BeanUtils.populate(user, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用Service中的修改方法
        UserService userService = new UserService();
        boolean b = userService.setUserById(user);
        //根据返回值跳转页面
        if (b) {
            //表示正确 跳转到正确的页面
            request.getRequestDispatcher("update_ok.jsp").forward(request, response);
        } else {
            //表示错误 跳转到错误的页面
            request.getRequestDispatcher("update_sb.jsp").forward(request, response);
        }
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
